import gtk,gobject

COL_NAME = 1

class treeview_helper:
    def __init__(self,treeview):
        try:
            if self.already == True:pass
        except:
            self.already = True
            self.treeview = treeview
            # Set up one column
            column = gtk.TreeViewColumn("Name"
                    , gtk.CellRendererText(),
                    text=COL_NAME) # position of column
            self.treeview.append_column(column)

        # Create a model for the data
        self.treestore = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING, \
                                         gobject.TYPE_STRING)

        self.folders = {}

        # Full list of categories - the first string is shown in the treeview,
        # the second is for reference in code
        # (Split for multilingual reasons)
        fullcats = [["Game Screens","gamescreens"],["Game Objects","gameobjects"], 
                    ["Workstates","workstates"],["Sprites","sprites"]]


        # Create our object categories
        for cat in fullcats:
            self.treestore.append(None,[None,cat[0],cat[1]])

        # Tell the treeview we want to display this data
        self.treeview.set_model(self.treestore)

    def clear(self):
        self.__init__(self.treeview)

    def init_item_list(self,widget=None):
        pass

    def update_item_list(self,widget=None):

        # Check for items that don't exist any more:
        self.delete_old_items("workstates",self.game.workstates)
        self.delete_old_items("gamescreens",self.game.gamescreens)
        self.delete_old_items("gameobjects",self.game.gameobjects)
        self.delete_old_items("sprites",self.game.sprites)

        # Add New items:
        self.add_new_items("workstates",self.game.workstates)
        self.add_new_items("gamescreens",self.game.gamescreens)
        self.add_new_items("gameobjects",self.game.gameobjects)
        self.add_new_items("sprites",self.game.sprites)


    def add_new_items(self,foldername,gamedict):
        """Adds any items that are keys in a dict, but not in a folder"""
        for name in gamedict:
            folder = self.get_folder(foldername)
            if not self.item_exists( name = name , folder = folder ):
                self.treestore.append(folder.iter, [gamedict[name], name,""] )

        
    def delete_old_items(self,foldername,gamedict):
        """Deletes any items that are in a folder but not in a dict"""
        folder = self.get_folder(foldername)
        for row in folder.iterchildren():
            if not gamedict.has_key(row[1]):
                self.treestore.remove(row.iter)

    def populate_from_game(self,game):
        """Set a game to use and populates the Treeview"""
        self.game = game
        self.update_item_list()

    def get_folder(self,name):
        """Gets a specified folder"""
        for item in self.treestore:
            if item[2] == name:
                return item

    def item_exists(self,name,folder):
        """Checks if an item exists within a folder"""
        for row in folder.iterchildren():
            if row[1] == name:
                return True
        return False

        
        
